package cn.kgcnews.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.kgcnews.dao.NewsDao;
import cn.kgcnews.pojo.NewsDetail;
import cn.kgcnews.utils.BaseDao;

public class NewsDaoImpl extends BaseDao implements NewsDao {

	@Override
	public List<NewsDetail> queryNewsTop14() throws Exception {
		List<NewsDetail> list = new ArrayList<NewsDetail>();
		String sql = "SELECT nid,ntitle,ncreateDate FROM news_detail ORDER BY ncreateDate DESC LIMIT 14";
		ResultSet rs = super.executeQuery(sql, null);
		while (rs != null && rs.next()) {
			NewsDetail news = new NewsDetail();
			news.setId(rs.getInt("nid"));
			news.setTitle(rs.getString("ntitle"));
			news.setCreateDate(rs.getTimestamp("ncreateDate"));
			list.add(news);
		}
		super.closeResource(rs);
		return list;
	}

	@Override
	public NewsDetail queryNewsByNId(String nid) throws Exception {
		NewsDetail news = null;
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT nid,ntid,ntitle,");
		sql.append("nauthor,ncreateDate,npicPath,");
		sql.append("ncontent,nmodifyDate,nsummary");
		sql.append(" FROM news_detail WHERE nid=?");
		Object[] params = { nid };
		ResultSet rs = super.executeQuery(sql.toString(), params);
		if (rs != null && rs.next()) {
			news = new NewsDetail();
			news.setId(rs.getInt("nid"));
			news.settId(rs.getInt("ntid"));
			news.setTitle(rs.getString("ntitle"));
			news.setAuthor(rs.getString("nauthor"));
			news.setCreateDate(rs.getTimestamp("ncreateDate"));
			news.setPicPath(rs.getString("npicPath"));
			news.setContent(rs.getString("ncontent"));
			news.setModifyDate(rs.getTimestamp("nmodifyDate"));
			news.setSummary(rs.getString("nsummary"));
		}
		return news;
	}

	@Override
	public int addNews(NewsDetail news) throws Exception {
		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO news_detail(ntid,ntitle,nauthor");
		sql.append(",ncreatedate,npicpath,ncontent,nsummary)");
		sql.append(" VALUES(?,?,?,?,?,?,?)");

		Object[] params = { news.gettId(), news.getTitle(), news.getAuthor(),
				news.getCreateDate(), news.getPicPath(), news.getContent(),
				news.getSummary() };
		return super.executeUpdate(sql.toString(), params);
	}

	@Override
	public int queryTotalRecord(String searchTitle,int searchTId) throws Exception {
		List paramsList=new ArrayList();
		int row=0;
		StringBuffer sql=new StringBuffer();
		sql.append("SELECT COUNT(1) FROM news_detail");
		sql.append(" where 1=1 ");
		if(searchTitle!=null && !"".equals(searchTitle)){
			sql.append(" and ntitle like ?");
			paramsList.add("%"+searchTitle+"%");
		}
		if(searchTId>0){
			sql.append(" and ntid=?");
			paramsList.add(searchTId);
		}
		Object[] params=paramsList.toArray();
		ResultSet rs=super.executeQuery(sql.toString(), params);
		if(rs!=null && rs.next()){
			row=rs.getInt(1);
		}
		super.closeResource(rs);
		return row;
	}

	@Override
	public List<NewsDetail> queryNewsByOffset(int offSet, int pageSize,String searchTitle,int searchTId)
			throws Exception {
		List<NewsDetail> list = new ArrayList<NewsDetail>();
		List paramsList=new ArrayList();
		StringBuffer sql=new StringBuffer();
		sql.append("SELECT nid,ntitle, nauthor,ncreateDate");
		sql.append(" FROM news_detail where 1=1 ");
		if(searchTitle!=null && !"".equals(searchTitle)){
			sql.append(" and ntitle like ?");
			paramsList.add("%"+searchTitle+"%");
		}
		if(searchTId>0){
			sql.append(" and ntid=?");
			paramsList.add(searchTId);
		}
		sql.append(" ORDER BY ncreateDate DESC LIMIT ?,? ");
		//System.out.println("发送的sql:"+sql);
		paramsList.add(offSet);
		paramsList.add(pageSize);
		Object[] params=paramsList.toArray();
		ResultSet rs = super.executeQuery(sql.toString(), params);
		while (rs != null && rs.next()) {
			NewsDetail news = new NewsDetail();
			news.setId(rs.getInt("nid"));
			news.setTitle(rs.getString("ntitle"));
			news.setAuthor(rs.getString("nauthor"));
			news.setCreateDate(rs.getTimestamp("ncreateDate"));
			list.add(news);
		}
		super.closeResource(rs);
		return list;
	}
	@Override
	public int delDetailByNid(Integer nid) throws Exception{
		//int row=-1;
		String sql3="DELETE FROM news_comments WHERE cnid=?";
		String sql="DELETE FROM news_detail WHERE nid=? ";
		/*String sql1="SET FOREIGN_KEY_CHECKS = 0";
		String sql2="SET FOREIGN_KEY_CHECKS = 1";*/
		Object[] params={nid};
		int result1=this.executeUpdate(sql3, params);
		int result2=this.executeUpdate(sql, params);
		if(result1>0&&result2>0){
			return result2;
		}
		
		
		//if(this.executeUpdate(sql1, null)==0){
			
		//}				
		return 0;
	}

}
